package p2pscrabble;

import java.awt.FlowLayout;
import java.awt.Point;

import javax.swing.JPanel;

public class LetterPanel extends JPanel {
	private LetterTile letterTile[];
	private boolean remove = true;

	public LetterPanel() {
		java.awt.Dimension size = new java.awt.Dimension(27 * 8, 56);
		this.setMinimumSize(size);
		this.setPreferredSize(size);
		this.setMaximumSize(size);

		new FlowLayout(FlowLayout.LEFT, 0, 0);
		this.setLayout(null);

		this.setTransferHandler(new MyTransferHandler());

		letterTile = new LetterTile[7];
	}

	public void drawTiles(char letter) {
		if (letter == '-')
			return;

		for (int i = 0; i < 7; i++) {
			if (letterTile[i] != null)
				continue;
			if (letter != ' ')
				letterTile[i] = new LetterTile(letter, false, false, false);
			else
				letterTile[i] = new LetterTile(letter, false, true, false);
			this.add(letterTile[i]);
			break;
		}
	}

	public void rearrangeTiles() {
		for (int i = 0; i < 7; i++) {
			if (letterTile[i] != null) {
				letterTile[i].setBounds(i * 27, 0, 27, 27);
				letterTile[i].setVisible(true);
			}
		}
	}

	public void dropTile(Point source, int x, int y) {
		int i = getWhichTile(source);
		letterTile[i].setLocation(x, y);
	}

	public void dropTileFromBoard(LetterTile lt, int x, int y) {
		int i;
		for (i = 0; i < 7; i++) {
			if (letterTile[i] == null) {
				letterTile[i] = new LetterTile(lt.getLetter(), false, lt
						.isBlank(), false);
				this.add(letterTile[i]);
				letterTile[i].setBounds(x, y, 27, 27);
				break;
			}
		}
	}

	public void swapTile(Point source, Point dest) {
		if (source.x == dest.x && source.y == dest.y) {
			return;
		}

		int tile1 = getWhichTile(source);
		int tile2 = getWhichTile(dest);

		letterTile[tile1].setLocation(dest);
		letterTile[tile2].setLocation(source);
	}

	public void swapTileFromBoard(char letter, boolean isBlank, Point drop) {
		int i = getWhichTile(drop);
		this.remove(letterTile[i]);
		letterTile[i] = new LetterTile(letter, false, isBlank, false);
		letterTile[i].setBounds(drop.x, drop.y, 27, 27);
		this.add(letterTile[i]);
		repaint();
	}

	public void removeTile(LetterTile lt) {
		int i = getWhichTile(lt.getLocation());
		this.remove(letterTile[i]);
		letterTile[i] = null;
	}

	private int getWhichTile(Point source) {
		for (int i = 0; i < 7; i++) {
			if (letterTile[i] == null)
				continue;

			Point initPoint = letterTile[i].getLocation();
			if (initPoint.x == source.x && initPoint.y == source.y) {
				return i;
			}
		}
		return -1;
	}

	public boolean getRemove() {
		return remove;
	}

	public void setRemove(boolean rem) {
		remove = rem;
	}

	public char[] getLetters() {
		char[] letters = new char[7];
		for (int i = 0; i < 7; i++) {
			if (letterTile[i] == null)
				letters[i] = '-';
			else
				letters[i] = letterTile[i].getLetter();
		}
		return letters;
	}

	public void setLetterTiles(char[] letters) {
		for (int i = 0; i < 7; i++) {
			if (letterTile[i] != null) {
				this.remove(letterTile[i]);
				letterTile[i] = null;
			}
			drawTiles(letters[i]);
		}
		rearrangeTiles();
	}
}
